package com.rightsidetech.http.controller.admin.material;

import com.github.pagehelper.PageInfo;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.ActivityMaterialAddReq;
import com.rightsidetech.system.dtos.request.ActivityMaterialOperateReq;
import com.rightsidetech.system.dtos.request.ActivityMaterialReq;
import com.rightsidetech.system.dtos.request.ActivityMaterialUpdateReq;
import com.rightsidetech.system.dtos.response.material.ActivityMaterialDetailsResp;
import com.rightsidetech.system.dtos.response.material.ActivityMaterialResp;
import com.rightsidetech.system.service.SysMaterialService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;

/**
 * @Package: com.rightsidetech.http.controller.admin.material
 * @ClassName: ActivityMaterialController
 * @Author: yinj
 * @CreateTime: 2021/12/13 9:44
 * @Description: 活动素材相关
 */
@RestController
@Api(tags = "素材管理相关api")
@RequestMapping("/admin/material")
@ShenyuSpringMvcClient(path = "/admin/material/**")
public class MaterialSendController extends BaseController {

    @Resource
    private SysMaterialService sysMaterialService;


    @Log
    @GetMapping("/page")
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @ApiOperation("素材列表")
    public ResponseData<PageInfo<ActivityMaterialResp>> page(ActivityMaterialReq request) throws Exception{

        ConstraintViolation validate = BeanValidator.validate(request);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        return sysMaterialService.page(request);
    }


    @Log
    @PostMapping("/add")
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @ApiOperation("添加")
    public ResponseData add(@RequestBody ActivityMaterialAddReq request){

        ConstraintViolation validate = BeanValidator.validate(request);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysMaterialService.add(request,loginInfoId);
    }


    @Log
    @PostMapping("/update")
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @ApiOperation("修改")
    public ResponseData update(@RequestBody ActivityMaterialUpdateReq request){

        ConstraintViolation validate = BeanValidator.validate(request);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysMaterialService.update(request,loginInfoId);
    }

    @Log
    @PostMapping("/operate")
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @ApiOperation("操作: 1发送/2删除")
    public ResponseData operate(@RequestBody ActivityMaterialOperateReq request){

        ConstraintViolation validate = BeanValidator.validate(request);
        if (validate != null) {
            return new ResponseData<>(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
        }
        if (request.getOperateType() != 1 && request.getOperateType() != 2){
            return ResponseData.fail(CodeIdEnum.IllegalParameter);
        }
        Long loginInfoId = Long.parseLong(getAttribute());
        return sysMaterialService.operate(request,loginInfoId);
    }

    @Log
    @GetMapping("/details")
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @ApiOperation("详情")
    public ResponseData<ActivityMaterialDetailsResp> details(Long id) throws Exception{
        return sysMaterialService.details(id);
    }


}
